const nodemailer = require("nodemailer");

// 创建一个发送邮件的对象
// 在第三方包下 nodemailer/lib/well-know/service.json
let transporter = nodemailer.createTransport({
  host: "smtp.qq.com",
  port: 465,
  secure: true, // true for 465, false for other ports
  auth: {
    user: "1444936552@qq.com", // generated ethereal user
    pass: "qwviqzsfdpcpgjfi", // generated ethereal password
  },
});

function sendMailCode(mailCode, mail) {
  // 发送邮件的信息
  const mailMsg = {
    from: '"Fred Foo 👻" <1444936552@qq.com>', // sender address
    to: mail, // list of receivers
    subject: "Hello ✔", // Subject line
    text: `欢迎注册小姜的网站,您的验证码是:${mailCode},有效期5分钟哦~`, // plain text body
  };

  return new Promise((resolve, reject) => {
    transporter
      .sendMail(mailMsg)
      .then((data) => {
        resolve(data);
      })
      .catch((err) => {
        reject(err);
      });
  });
}
// send("0222", "1444936552@qq.com");

module.exports = { sendMailCode };
